/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.uoc.tdp.pac3.bo.impl;

import edu.uoc.tdp.pac3.bo.IIncidenciaBO;
import edu.uoc.tdp.pac3.domain.Area;
import edu.uoc.tdp.pac3.domain.Request;
import edu.uoc.tdp.pac3.domain.RequestNote;
import edu.uoc.tdp.pac3.domain.RequestType;
import edu.uoc.tdp.pac3.domain.User;
import edu.uoc.tdp.pac3.persistence.dao.IAreaDao;
import edu.uoc.tdp.pac3.persistence.dao.IRequestDao;
import edu.uoc.tdp.pac3.persistence.dao.IRequestNoteDao;
import edu.uoc.tdp.pac3.persistence.dao.IUserDao;
import edu.uoc.tdp.pac3.persistence.dao.impl.AreaDao;
import edu.uoc.tdp.pac3.persistence.dao.impl.RequestDao;
import edu.uoc.tdp.pac3.persistence.dao.impl.RequestNoteDao;
import edu.uoc.tdp.pac3.persistence.dao.impl.UserDao;
import edu.uoc.tdp.pac3.to.HistoricIncidenciaTO;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Gerard
 */
public class IncidenciaBO implements IIncidenciaBO {

    private IRequestDao requestDao;
    private IRequestNoteDao requestNoteDao;
    private IUserDao userDao;
    private IAreaDao areaDao;

    public IncidenciaBO() {
        requestDao = new RequestDao();
        requestNoteDao = new RequestNoteDao();
        userDao = new UserDao();
        areaDao = new AreaDao();
    }

    public IncidenciaBO(IRequestDao requestDao, IRequestNoteDao requestNoteDao) {
        this.requestDao = requestDao;
        this.requestNoteDao = requestNoteDao;
    }

    public boolean novaIncidencia(Request request) {
        return requestDao.insert(request);
    }

    public boolean modificarIncidencia(Request request, RequestNote rqNote) {
        boolean updated = requestDao.update(request);
        if (updated && rqNote != null) {
            updated = requestNoteDao.insert(rqNote);
        }
        return updated;
    }

    public List<Request> getIncidenciesActives() {
        return requestDao.findRequestsByType(RequestType.NOTSOLVED);
    }

    public List<Request> getIncidenciesResoltes() {
        return requestDao.findRequestsByType(RequestType.SOLVED);
    }

    public List<Request> getIncidencies(Date date) {
        return null;
    }

    public boolean resoldreIncidencia(Request request) {
        request.setRequestType(RequestType.SOLVED);
        return requestDao.update(request);
    }

    public List<HistoricIncidenciaTO> getHistorics(RequestType requestType) {
        List<HistoricIncidenciaTO> historics = null;
        List<Request> requests = requestDao.findRequestsByType(requestType);
        if (requests != null && requests.size() > 0) {
            historics = new ArrayList<HistoricIncidenciaTO>();
            for (Request request : requests) {
                HistoricIncidenciaTO historic = new HistoricIncidenciaTO();
                historic.setRequest(request);
                historic.setRequestNotes(requestNoteDao.getRequestNotes(request.getId(), request.getUserId()));
                historics.add(historic);
            }
        }
        return historics;
    }

    public List<User> getUsers() {
        return userDao.findAll();
    }

    public List<Area> getAreas() throws RemoteException {
        return areaDao.findAll();
    }

    public boolean resoldreIncidencia(Integer idRequest, Integer idUserNote, String userNote) throws RemoteException{
        boolean result = false;
        Request request = requestDao.findById(idRequest);
        result = resoldreIncidencia(request);
        
        RequestNote requestNote = new RequestNote();
        requestNote.setDescription(userNote);
        requestNote.setUserId(idUserNote);
        requestNote.setCreationDate(new Date());
        requestNote.setRequestId(idRequest);
        
        if(result){
            result = requestNoteDao.insert(requestNote);
        }
        return result;
    }
    

    public List<RequestNote> getRequestNoteFromRequest(Request request) throws RemoteException{
        return requestNoteDao.getRequestNotes(request.getId());
    }
}
